// import {any} from "vue-types";

const appraise = [
  {},
  { id: 1, SelfAss: 50, PeerEva: 50 },
  {
    id: 2,
    SelfAss: 20,
    PeerEva: 30,
    Parents: 50
  },
  {
    id: 3,
    SelfAss: 25,
    PeerEva: 25,
    Parents: 25,
    TeacherEva: 25,
    Other: 34
  },
  {
    id: 4,
    SelfAss: 50,
    PeerEva: 50
  },
  {
    id: 5,
    SelfAss: 50,
    PeerEva: 50
  }
]
const metrics = [
  {
    id: 1,
    oneMet: '品德发展',
    twoMet: '理想信念',
    threeMet:
      '了解党史国情，珍视国家荣誉，铸牢中华民族共同体意识，爱党爱国爱人民爱社会主义。立志听党话、跟党走，从小树立为实现中华民族伟大复兴的中国梦而努力奋斗的志向。',
    score: 3,
    appraise: appraise[0]
  },
  {
    id: 2,
    oneMet: '品德发展',
    twoMet: '理想信念',
    threeMet:
      '会唱国歌，积极参加升国旗仪式;积极参加重要节日、纪念日主题教育活动，积极参加少先队、共青团活动。',
    score: 5,
    appraise: appraise[1]
  },
  {
    id: 3,
    oneMet: '品德发展',
    twoMet: '理想信念',
    threeMet:
      '热爱并努力学习中华优秀传统文化、革命文化和社会主义先进文化，传承红色基因，增强“四个自信”;积极向英雄模范和先进典型人物学习。',
    score: 3,
    appraise: appraise[2]
  },
  {
    id: 4,
    oneMet: '品德发展',
    twoMet: '理想信念',
    threeMet: '养成规则意识，遵守校规校纪，遵守法律法规、社会公德和公共秩序。',
    score: 4,
    appraise: appraise[3]
  },
  {
    id: 5,
    oneMet: '品德发展',
    twoMet: '理想信念',
    threeMet: '养成规则意识，遵守校规校纪，遵守法律法规、社会公德和公共秩序。',
    score: 4,
    appraise: appraise[4]
  }
]

const metric = [
  {
    id: 1,
    semester: '2022-2023第二学期',
    metrics
  },
  {
    id: 1,
    semester: '2022-2023第一学期',
    metrics
  }
]

export default [
  {
    url: '/metricsList',
    method: 'get',
    response: (params) => {
      const total = metric.length
      const current = params.query.current
      const size = params.query.size
      const right = size * current
      const left = (current - 1) * size
      const data = metric.slice(left, right)
      return {
        code: '0000',
        total,
        data
      }
    }
  },
  {
    url: '/addmetrics',
    method: 'post',
    response: (data) => {
      //console.log(data.body.metrics)
      metric.unshift({
        // metrics: formArr,
        id: metric.length + 1,
        semester: data.body.semester,
        metrics
      })

      metrics.unshift({
        appraise: {
          id: appraise.length + 1,
          Other: data.body.Other,
          Parents: data.body.Parents,
          PeerEva: data.body.PeerEva,
          SelfAss: data.body.SelfAss,
          TeacherEva: data.body.TeacherEva
        },
        oneMet: data.body.oneMet,
        score: data.body.score,
        threeMet: data.body.threeMet,
        twoMet: data.body.twoMet,
        id: metrics.length + 1
      })

      appraise.unshift({
        id: appraise.length + 1,
        Other: data.body.Other,
        Parents: data.body.Parents,
        PeerEva: data.body.PeerEva,
        SelfAss: data.body.SelfAss,
        TeacherEva: data.body.TeacherEva
      })
      return {
        code: '0000',
        data,
        status: '添加成功'
      }
    }
  },
  {
    url: '/revisemetrics',
    method: 'post',
    response: (body) => {
      for (let i = 0; i < metric[0].metrics.length; i++) {
        if (metric[0].metrics[i].id == body.query.id) {
          metric[0].metrics[i] = body.query
          // metric[0].metrics[i].appraise = appraise[0]
          let SelfAss = 0
          let Parents = 0
          let PeerEva = 0
          let Other = 0
          let TeacherEva = 0
          console.log(metric[0].metrics[i])
          console.log(safeGet(metric[0].metrics[i], 'Parents'))
          if (safeGet(metric[0].metrics[i], 'SelfAss')) SelfAss = body.query.SelfAss
          if (safeGet(metric[0].metrics[i], 'Parents')) Parents = body.query.Parents
          if (safeGet(metric[0].metrics[i], 'PeerEva')) PeerEva = body.query.PeerEva
          if (safeGet(metric[0].metrics[i], 'Other')) Other = body.query.Other
          if (safeGet(metric[0].metrics[i], 'TeacherEva')) TeacherEva = body.query.TeacherEva
          metric[0].metrics[i].appraise = { id: 0, SelfAss, Parents, PeerEva, Other, TeacherEva }
          console.log({ id: 0, SelfAss, Parents, PeerEva, Other, TeacherEva })
        }
      }
      return {
        code: '0000',
        data: metric,
        status: '修改成功'
      }
    }
  },
  {
    url: '/delmetrics',
    method: 'get',
    response: (params) => {
      console.log(params.query.id)
      const id = params.query.id
      const deleteIndex = metric.findIndex((item) => {
        return item.id == id
      })
      metrics.splice(deleteIndex, 1)
      return {
        code: '0000',
        params,
        data: '删除成功'
      }
    }
  }
]

const safeGet = (data, path) => {
  if (data == undefined) return false

  const pathArr = path.split('.')
  let result = data

  for (let i = 0; i < pathArr.length; i++) {
    if (result[pathArr[i]] == undefined) return false
    result = result[pathArr[i]]
  }
  return true
}
